package net.kesumu.general.android.opengl.object;

import static android.opengl.GLES20.GL_TRIANGLE_FAN;
import static android.opengl.GLES20.glDrawArrays;
import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.*;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;

import java.util.List;

import net.kesumu.general.android.opengl.object.Geometry.Circle;
import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.object.ObjectBuilder.DrawCommand;
import net.kesumu.general.android.opengl.object.ObjectBuilder.GeneratedData;
import net.kesumu.general.android.opengl.program.TextureShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class YushabuPhoto {
    
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT 
        + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    
    public static final int INITIATED = 0,
                            FOCUSING = 1,
                            FOCUSED = 2,
                            UNFOCUSING = 3,
                            UNFOCUSED = 4;
    
    public int state;
    public final int userId;
    
    public Point circleCenter;
    public final int texture;
    
    private final VertexArray vertexArray;
    private final List<DrawCommand> drawList;
    
    private final VertexArray focusedVertexArray;
    private final List<DrawCommand> focusedDrawList;

    public YushabuPhoto(int userId, Point center, int texture, int numPoints) {
        this.userId = userId;
        this.state = INITIATED;
        this.texture = texture;
        this.circleCenter = center;

        Circle circle = new Circle(circleCenter, PHOTO_WIDTH/2f);
        
        GeneratedData generatedData = ObjectBuilder.createYushabuPhoto(circle, numPoints);
        
        vertexArray = new VertexArray(generatedData.vertexData);
        drawList = generatedData.drawList;
        
        Circle focusedCircle = new Circle(circleCenter, PHOTO_FOCUSED_FG_WIDTH/2f);
        
        GeneratedData focusedGeneratedData = ObjectBuilder.createYushabuPhoto(focusedCircle, numPoints);
        
        focusedVertexArray = new VertexArray(focusedGeneratedData.vertexData);
        focusedDrawList = focusedGeneratedData.drawList;
    }
    
    public void bindData(TextureShaderProgram textureProgram) {
        vertexArray.setVertexAttribPointer(
            0, 
            textureProgram.getPositionAttributeLocation(), 
            POSITION_COMPONENT_COUNT,
            STRIDE);
        
        vertexArray.setVertexAttribPointer(
            POSITION_COMPONENT_COUNT, 
            textureProgram.getTextureCoordinatesAttributeLocation(),
            TEXTURE_COORDINATES_COMPONENT_COUNT, 
            STRIDE);
    }
    
    public void focusedBindData(TextureShaderProgram textureProgram) {
        focusedVertexArray.setVertexAttribPointer(
            0, 
            textureProgram.getPositionAttributeLocation(), 
            POSITION_COMPONENT_COUNT,
            STRIDE);
        
        focusedVertexArray.setVertexAttribPointer(
            POSITION_COMPONENT_COUNT, 
            textureProgram.getTextureCoordinatesAttributeLocation(),
            TEXTURE_COORDINATES_COMPONENT_COUNT, 
            STRIDE);
    }
        
    public void draw() {
        for (DrawCommand drawCommand : drawList) {
            drawCommand.draw();
        }
    }
    
    public void focusedDraw() {
        for (DrawCommand drawCommand : focusedDrawList) {
            drawCommand.draw();
        }
    }

}
